<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true"
          >添加分类</el-button
        >
      </div>
      <!-- 表格区域 -->
      <el-table style="width: 100%" :data="ArtCateList" border stripe>
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              size="mini"
              @click="showEditDialog(row.id)"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="delArtCate(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加文章分类对话框 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="addVisible"
      width="35%"
      :close-on-click-modal="false"
      @closed="$refs.addRef.resetFields()"
    >
      <!-- 添加的表单 -->
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addRef"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input
            v-model="addForm.cate_name"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input
            v-model="addForm.cate_alias"
            minlength="1"
            maxlength="15"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addArtCate"
          >添 加</el-button
        >
      </span>
    </el-dialog>

    <!-- 修改文章分类对话框 -->
    <el-dialog
      title="修改文章分类"
      :visible.sync="editVisible"
      width="35%"
      :close-on-click-modal="false"
      @closed="$refs.editRef.resetFields()"
    >
      <!-- 修改的表单 -->
      <el-form
        :model="editForm"
        :rules="addRules"
        ref="editRef"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input
            v-model="editForm.cate_name"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input
            v-model="editForm.cate_alias"
            minlength="1"
            maxlength="15"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="editArtCate"
          >修 改</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data() {
    return {
      // addVisible控制文章分类对话框的显示和隐藏
      addVisible: false,
      editVisible: false,
      // 保存文章分类的列表数据
      ArtCateList: [],
      // 添加表单的数据对象
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 修改表单的数据对象
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.initArtCateList()
  },
  methods: {
    // 获取文章分类数据列表
    async initArtCateList() {
      const { data: res } = await this.$http.get('/my/cate/list')
      // console.log(res)
      if (res.code === 0) {
        this.ArtCateList = res.data
      }
    },
    // 添加文章分类
    addArtCate() {
      // 表单预校验
      this.$refs.addRef.validate(async(valid) => {
        if (!valid) return
        // 发起请求
        const { data: res } = await this.$http.post(
          '/my/cate/add',
          this.addForm
        )
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 关闭对话框
        this.addVisible = false
        // 重新请求列表数据
        this.initArtCateList()
      })
    },
    // 展示修改对话框
    async showEditDialog(id) {
      // 1. 获取分类的基本信息
      const { data: res } = await this.$http.get('/my/cate/info', {
        params: { id }
      })

      if (res.code === 0) {
        // 2. 为 data 中的 editForm 赋值
        this.editForm = res.data
        // 3. 展示对话框
        this.editVisible = true
      }
    },
    // 修改文案文章分类
    editArtCate() {
      // 1. 表单预验证
      this.$refs.editRef.validate(async(valid) => {
        if (!valid) return

        // 2. 发起请求
        const { data: res } = await this.$http.put(
          '/my/cate/info',
          this.editForm
        )
        // 3. 修改失败
        if (res.code !== 0) return this.$message.error(res.message)
        // 4. 修改成功：提示用户、关闭对话框、刷新列表数据
        this.$message.success(res.message)
        this.editVisible = false
        this.initArtCateList()
      })
    },
    // 删除文章分类
    delArtCate(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          // 发起请求删除对应的分类
          const { data: res } = await this.$http.delete('/my/cate/del', {
            params: { id }
          })

          // 判断是否删除成功
          if (res.code !== 0) return this.$message.error(res.message)
          this.$message.success(res.message)
          this.initArtCateList()
        })
        .catch((err) => err)
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-table {
  font-size: 12px;
}

// 全局重置 el-table 的样式
.el-table thead {
  color: #000;
}

.el-dialog__body {
  padding: 10px 20px;
}
</style>
